
* Generates a random number to omit 20% of the observations randomly
clear all
set mem 50m
* cd "..."
use "DATA_COMPILED_ATOP_20151118.dta"
set more off 

set seed 9348

gen sample1 = 0
replace sample1 = 1 if year <= 2004
label var sample1 "All States"
gen sample2 = 0
replace sample2 = 1 if year <= 2004 & usally == 1 & NATO == 0
label var sample2 "Non-NATO Allies"
gen sample3 = 0
replace sample3 = 1 if year <= 2004 & NATO == 1
label var sample3 "NATO Allies"
gen sample4 = 0
replace sample4 = 1 if year <= 2004 & usally == 0
label var sample4 "Non-Allies"

label var interaction "Troops $\times$ Troops Spatial Mean"
label var meanregionalallies "Host-State Allies (Spatial Mean)"
label var meanregionalusallies "US Allies (Spatial Mean)"

postutil clear
tempname modelfit
postfile `modelfit' str20 Sample r21 r22 sum1 sum2 using modelfit, replace

foreach var of varlist sample1-sample4 {

gen prob1 = .
gen prob2 = .

xtpcse defenseburden l.lntroops l.lntroops_spmean l.interaction l.spatial_lag l.polity l.growth l.lntpop l.IMR l.threat_environment war movav3 borderstates civilwar l.meanregionalallies l.meanregionalusallies if `var'==1, corr(ar1) pairwise

keep if e(sample)

gen double i = runiformint(1,5)
tab i
gen pr1 = .
gen pr2 = .

xtpcse defenseburden l.polity l.growth l.lntpop l.IMR l.threat_environment war movav3 borderstates civilwar if year<2004 & i!=1, corr(ar1) pairwise

predict xb
replace pr1 = xb if i==1
drop xb
scalar r21 = e(r2)
****
scalar sum1 = e(rss)

xtpcse defenseburden l.lntroops l.lntroops_spmean l.interaction l.spatial_lag l.polity l.growth l.lntpop l.IMR l.threat_environment war movav3 borderstates civilwar l.meanregionalallies l.meanregionalusallies if year<2004 & i!=1, corr(ar1) pairwise

predict xb
replace pr2 = xb if i==1
drop xb
****
scalar sum2 = e(rss)

gen res1 = (defenseburden-pr1)^2
gen res2 = (defenseburden-pr2)^2

sum res1 
*scalar sum1 = r(sum)
sum res2
*scalar sum2 = r(sum)
scalar r22 = e(r2)
post `modelfit' ("`:variable label `var''") (r21) (r22) (sum1) (sum2)
scalar drop _all

use "~/Dropbox/Projects/Troop Deployments and Spatial Effects/Data/Raw Data Files/DATA_COMPILED_ATOP_20151118.dta", clear
gen sample1 = 0
replace sample1 = 1 if year <= 2004
label var sample1 "All States"
gen sample2 = 0
replace sample2 = 1 if year <= 2004 & usally == 1 & NATO == 0
label var sample2 "Non-NATO Allies"
gen sample3 = 0
replace sample3 = 1 if year <= 2004 & NATO == 1
label var sample3 "NATO Allies"
gen sample4 = 0
replace sample4 = 1 if year <= 2004 & usally == 0
label var sample4 "Non-Allies"

}

postclose `modelfit'
clear
use modelfit

foreach var of varlist r21 r22 sum1 sum2 {
format `var' %9.4fc
}

listtex Sample sum1 sum2 using table_modelcompare.tex, rstyle(tabular) replace ///
delimiter("&") headlines("\begin{tabular}{l c c}" "\hline\hline" "Sample & Base RSS & Full RSS \\ \hline" ) footlines("\hline\hline" "\end{tabular}") type 
